package com.water.wfo.component.api.entity.response;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.water.wfo.component.api.entity.Response;
import com.water.wfo.component.api.enums.ErrorEnum;
import com.water.wfo.component.constant.HttpStatusConstants;
import lombok.Data;

import java.io.Serializable;

/**
 * packageName com.water.wfo.component.api.entity.response
 *
 * @author junhuili
 * @version JDK 8
 * @date 2024/5/18
 * @description TODO
 */
@Data
public class BasicResponse<T> implements Response<T>, Serializable {
    private static final long serialVersionUID = 4208662314860204361L;
    /*
     * 响应状态（success：成功， fail：失败）
     */
    protected String status = "fail";
    /*
     * 提示信息
     */
    protected String message;
    /*
     * 详细业务code
     */
    protected String code;
    /*
     * 业务数据
     */
    protected T result;

    /**
     * 响应是否成功
     *
     * @return
     */
    @JsonIgnore
    public boolean isSuccess(){
        return HttpStatusConstants.SUCCESS.equals(this.status);
    }

    /**
     * fail
     * @return
     */
    public BasicResponse<T> buildFail(){
        return this.buildFail("");
    }

    /**
     * fail
     * @param message
     * @return
     */
    public BasicResponse<T> buildFail(String message){
        return this.buildFail("", message);
    }

    /**
     * fail
     * @param code
     * @param message
     * @return
     */
    public BasicResponse<T> buildFail(String code, String message){
        return this.buildFail(code, message, null);
    }

    /**
     * fail
     * @param message
     * @param result
     * @return
     */
    public BasicResponse<T> buildFail(String code, String message, T result){
        return this.buildResponse(HttpStatusConstants.FAIL, code, message, result);
    }

    /**
     * fail
     * @param errorEnum
     * @return
     */
    public BasicResponse<T> buildFail(ErrorEnum errorEnum) {
        return this.buildFail(errorEnum.getErrCode(), errorEnum.getErrMsg());
    }

    /**
     * success
     *
     * @return
     */
    public BasicResponse<T> buildSuccess(){
        return this.buildSuccess("");
    }

    /**
     * success
     *
     * @param message
     * @return
     */
    public BasicResponse<T> buildSuccess(String message){
        return this.buildSuccess("", message, null);
    }

    /**
     * success
     * @param result
     * @return
     */
    public BasicResponse<T> buildSuccess(T result){
        return this.buildSuccess("", "", result);
    }

    /**
     * success
     *
     * @param message
     * @return
     */
    public BasicResponse<T> buildSuccess(String code, String message){
        return this.buildSuccess(code, message, null);
    }

    /**
     * success
     *
     * @param message
     * @param result
     * @return
     */
    public BasicResponse<T> buildSuccess(String message, T result){
        return this.buildSuccess(null, message, result);

    }

    /**
     * success
     *
     * @param message
     * @param result
     * @return
     */
    public BasicResponse<T> buildSuccess(String code, String message, T result){
        return this.buildResponse(HttpStatusConstants.SUCCESS, code, message, result);
    }

    /**
     * 响应
     * @param status
     * @param code
     * @param message
     * @param result
     * @return
     */
    protected BasicResponse<T> buildResponse(String status, String code, String message, T result){
        this.status = status;
        this.code = code;
        this.message = message;
        this.result = result;
        return this;
    }
}
